Changed startup sequence and added daemon support