Long pull request

Hi, fixed 3 issues. Pull request contains several commits, but the commits themselves are short and specific. Will be more careful about smaller pull requests from now on.