Major changes
Switched to using functions (more to come in future)
Slightly improved error handling