more tests and refactoring

added more tests and did some refactoring. small performance
improvements.