Code Cleanup

Removed some duplicate code through decomposition.