Restructured internal code and added coloured output

Added functions to script to simplify logic, added coloured output,
redirected errors to STDERR, added exit through any non-zero code