freshest version with a lot of fixes by @slaFFik