improved code

had few bugs