Diff from Stefan Hagen <sh+ports at codevoid dot de> with tweaks from Omar Polo <op at omarpolo dot com>.