function changes + better error handling