Rough working version. Have to test and clean code