fixed errors, added null check, general cleanup