Proposed library refactoring

This is still missing tests, I'm going to rewrite then tomorrow.. :)