Cleanup and eliminate duplicate code