reorganising the code base and various bug fixes