Merge pull request #316 from kyuri/upstream

Small refactoring and code cleaning
Small fixes