Reworked funtion