Cleaning code and minor fixes