Cleaning up the code and general improvements in line with expected behaviour