Final version, seems to be working.