Fixed glitches

todo:
- Comment and clean up code. Do pull request.