Fixed bug and removed repetetive code