Add more exception handling, little code cleanup