Added tests and refactored a bit.