Minor refactoring. Added proper testing.