Added main.
Split stuff into more functions. Added optional arg