Improved flow, error handling, functions