Added tests and minor refactor