cleanup and removed duplicate code fragments