merged Draknet's version with my small additions