Replaced missing code + cleanup