final version. seems to work :)