Big rewrite, changed from huge if/elif/else block to modular detail