Better error handling, needs thorough testing