more tests and refactoring

Hi there,

I have added more tests and did some refactoring and small performance improvements.