Removed size_t additions and unused variabless
Added error check