Refactor elephant code.

Organise by functions and separate into two files.