(fix) grouped functionality into fewer functions
(fix) better error handling