modified and fixed code to work alright