Refactored code; get functions; better errors