Cleaning the code and added some new functions