improved code style and added some comments