Moved and cleaned function