Major refactoring of the entire app. Fixes #4 incidentally.