Improved code slightly