Removed duplicated code and cleanup it