Fixed indents.
Added error handling.
Simplified.