Added more tests and optimized code