Clean code, add error handling -- Fix #2