modified with few bugs