Semi refactored. Haven't really changed functionality too much, just improved readability.