trying to write in more functional style
rewritten some parsing functions because i think they were too ugly :)
much cleaner / more general now