Javascript functions redesigned, small fixes