fixed bug and improved code readability