Adding validation unit tests and refactor code for proper testing