cleaned up and added some changes from Radu