refactored lots of code, working on #25