Merged and clean up code