Revised code after bugfixes