cleaning the code and adding some missing test logic