Incorporating more of Przerwap's code... again.
He really needs to fix his damn git