Revert "Code cleanup and bug fixing"

This reverts commit 1f4c9bcfb54dd0f216927fe3886df785582e1559.