Removed duplicated code, minor refactors.