merged in code from master, after it was completely refactored