working solution
adds comments and error reporting