Implement cloning elements

Also refactored code... I need to remember to put different stuff in different commits.