Fix/returns

Several fixes to returns, and one to error reporting. Review by @DarkDimius. Supersedes #297.