Cleaned some code and added some doc