adapting some code