Better code. Added unit tests