better exception handling + code cleanup