Add unit tests and updated code as per code review