Fixed bug and reduced code