Code Cleanup

Removed duplicated code, using early bailout pattern and also general cleaning.