Tested and bugfixed as best as one coder can