some fixes

- reduced scope of some variables
- added null pointer check