removed bugs and cleared code duplicates