Added comments and entirely reconsidered the way errors are handled