Code cleanup, error handling